package com.yj_app.qualitytest.activity.home.homepresenter;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.yj_app.qualitytest.activity.home.homecontract.HomeContract;
import com.yj_app.qualitytest.activity.home.homemodel.HomeModel;
import com.yj_app.qualitytest.base.mvp.BasePresenterImpl;
import com.yj_app.qualitytest.callback.DialogCallback;
import com.yj_app.qualitytest.callback.SyncRes;
import com.yj_app.qualitytest.common.Config;
import com.yj_app.qualitytest.info.BaseInfo;
import com.yj_app.qualitytest.info.HdInfo;
import com.yj_app.qualitytest.info.HeadBoyInfo;
import com.yj_app.qualitytest.info.Loginfo;
import com.yj_app.qualitytest.info.QLineInfo;
import com.yj_app.qualitytest.info.QScoreInfo;
import com.yj_app.qualitytest.info.QStandardInfo;
import com.yj_app.qualitytest.info.QrelationshipInfo;
import com.yj_app.qualitytest.info.SubScoreInfo;
import com.yj_app.qualitytest.info.TeamInfo;
import com.yj_app.qualitytest.info.WorkSpaceInfo;
import com.yj_app.qualitytest.info.WorkerInfo;
import com.yj_app.qualitytest.info.up.MsgBean;
import com.yj_app.qualitytest.info.up.QualityVO;
import com.yj_app.qualitytest.util.GsonUtil;
import com.yj_app.qualitytest.util.PreferencesManager;
import com.yj_app.qualitytest.util.ToastUtils;

import org.litepal.LitePal;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class HomePresenter extends BasePresenterImpl<HomeContract.View, HomeContract.Model> implements HomeContract.Presenter {
    int count = 0;

    /**
     * 网络请求改为公用方法
     *
     * @param standardClass
     * @param corp
     * @param syncRes
     */
    @Override
    public void getData(String standardClass, String corp, SyncRes syncRes) {
        mModel.getCommon(data2json(standardClass,corp), new DialogCallback<String>((Activity) mView) {
            @Override
            public void onSuccess(Response<String> response) {

                String res = getMsgBean(response.body());
                Type type=new TypeToken<MsgBean<List<QStandardInfo>>>(){}.getType();
                MsgBean<List<QStandardInfo>>msgBean = GsonUtil.fromJson(res,type);
                if (!msgBean.isSuccess()){
                    return;
                }

                List<QStandardInfo> list = msgBean.getMapList();

                LitePal.deleteAll(QStandardInfo.class);

                LitePal.saveAll(list);
                count++;
                syncRes.syneSuccess(count);
//                Log.e("--", response.body().toString());


            }
        });

    }



    @Override
    public void SynQStandard(String qStandard, String corp, SyncRes syncRes) {


        mModel.getCommon(data2json(qStandard,corp), new DialogCallback<String>((Activity) mView) {
            @Override
            public void onSuccess(Response<String> response) {

                String res = getMsgBean(response.body());
                Type type=new TypeToken<MsgBean<List<QScoreInfo>>>(){}.getType();
                MsgBean<List<QScoreInfo>>msgBean = GsonUtil.fromJson(res,type);
                if (!msgBean.isSuccess()){
                    return;
                }
                List<QScoreInfo> qScoreInfoList = msgBean.getMapList();
                LitePal.deleteAll(QScoreInfo.class);
                LitePal.saveAll(qScoreInfoList);
                count++;
                syncRes.syneSuccess(count);
//                Log.e("--", response.body().toString());


            }
        });




    }

    @Override
    public void test(String qStandard, String corp, SyncRes syncRes) {
        mModel.test(qStandard, corp, new DialogCallback<String>((Activity) mView) {
            @Override
            public void onSuccess(Response<String> response) {
                Log.e("--", response.body().toString());
            }
        });
    }

    @Override
    public void syncQrelationship(String qrelationship, String corp, SyncRes syncRes) {


        mModel.getCommon(data2json(qrelationship,corp), new DialogCallback<String>((Activity) mView) {
            @Override
            public void onSuccess(Response<String> response) {

                String res = getMsgBean(response.body());
                Type type=new TypeToken<MsgBean<List<QrelationshipInfo>>>(){}.getType();
                MsgBean<List<QrelationshipInfo>>msgBean = GsonUtil.fromJson(res,type);
                if (!msgBean.isSuccess()){
                    return;
                }
                List<QrelationshipInfo> qrelationshipInfos = msgBean.getMapList();
                LitePal.deleteAll(QrelationshipInfo.class);
                LitePal.saveAll(qrelationshipInfos);
                count++;
                syncRes.syneSuccess(count);
//                Log.e("--", response.body().toString());


            }
        });

    }

    @Override
    public void syncWorker(String qWorker, String corp, SyncRes syncRes) {


        mModel.getCommon(data2json(qWorker,corp), new DialogCallback<String>((Activity) mView) {
            @Override
            public void onSuccess(Response<String> response) {

                String res = getMsgBean(response.body());
                Type type=new TypeToken<MsgBean<List<WorkerInfo>>>(){}.getType();
                MsgBean<List<WorkerInfo>>msgBean = GsonUtil.fromJson(res,type);
                if (!msgBean.isSuccess()){
                    return;
                }
                List<WorkerInfo> workerInfos = msgBean.getMapList();
                LitePal.deleteAll(WorkerInfo.class);
                LitePal.saveAll(workerInfos);
                count++;
                syncRes.syneSuccess(count);
//                Log.e("--", response.body().toString());


            }
        });

    }

    @Override
    public void syncHeadBoy(String qHead, String corp, SyncRes syncRes) {

        mModel.getCommon(data2json(qHead,corp), new DialogCallback<String>((Activity) mView) {
            @Override
            public void onSuccess(Response<String> response) {

                String res = getMsgBean(response.body());
                Type type=new TypeToken<MsgBean<List<HeadBoyInfo>>>(){}.getType();
                MsgBean<List<HeadBoyInfo>>msgBean = GsonUtil.fromJson(res,type);
                if (!msgBean.isSuccess()){
                    return;
                }
                List<HeadBoyInfo> headBoyInfos = msgBean.getMapList();
                LitePal.deleteAll(HeadBoyInfo.class);
                LitePal.saveAll(headBoyInfos);
                count++;
                syncRes.syneSuccess(count);
//                Log.e("--", response.body().toString());


            }
        });
    }

    @Override
    public void syncTeam(String qTeam, String corp, SyncRes syncRes) {


        mModel.syncTeam(qTeam, corp, new DialogCallback<BaseInfo<List<TeamInfo>>>((Activity) mView) {
            @Override
            public void onSuccess(Response<BaseInfo<List<TeamInfo>>> response) {
                LitePal.deleteAll(TeamInfo.class);
                List<TeamInfo> teamInfos = response.body().getList();
                LitePal.saveAll(teamInfos);
                count++;
                syncRes.syneSuccess(count);
                Log.e("--", response.body().toString());
            }
        });
    }

    @Override
    public void syncLines(String qLines, String corp, SyncRes syncRes) {

        mModel.getCommon(data2json(qLines,corp), new DialogCallback<String>((Activity) mView) {
            @Override
            public void onSuccess(Response<String> response) {

                String res = getMsgBean(response.body());
                Type type=new TypeToken<MsgBean<List<QLineInfo>>>(){}.getType();
                MsgBean<List<QLineInfo>>msgBean = GsonUtil.fromJson(res,type);
                if (!msgBean.isSuccess()){
                    return;
                }
                List<QLineInfo> qLineInfos = msgBean.getMapList();
                LitePal.deleteAll(QLineInfo.class);
                LitePal.saveAll(qLineInfos);
                count++;
                syncRes.syneSuccess(count);
//                Log.e("--", response.body().toString());


            }
        });

    }

    @Override
    public void syncChejian(String qWorkshop, String corp, SyncRes syncRes) {

        mModel.getCommon(data2json(qWorkshop,corp), new DialogCallback<String>((Activity) mView) {
            @Override
            public void onSuccess(Response<String> response) {

                String res = getMsgBean(response.body());
                Type type=new TypeToken<MsgBean<List<WorkSpaceInfo>>>(){}.getType();
                MsgBean<List<WorkSpaceInfo>>msgBean = GsonUtil.fromJson(res,type);
                if (!msgBean.isSuccess()){
                    return;
                }
                List<WorkSpaceInfo> workSpaceInfos = msgBean.getMapList();
                LitePal.deleteAll(WorkSpaceInfo.class);
                LitePal.saveAll(workSpaceInfos);
                count++;
                syncRes.syneSuccess(count);
//                Log.e("--", response.body().toString());


            }
        });

    }

    @Override
    protected HomeContract.Model createModel() {
        return new HomeModel();
    }
}
